package com.example.com.test.view

import android.content.Context
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.drawable.Drawable
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v4.graphics.drawable.DrawableCompat
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.MotionEvent.ACTION_DOWN
import android.view.animation.*
import android.widget.FrameLayout
import android.widget.ImageView
import com.example.com.test.R
import java.util.*

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class HeaderView : FrameLayout {
    companion object {
        private val INTERVAL: Int = 500
    }
    private var lastClickTime: Long = 0
    private var res: Drawable? =null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
         res=resources.getDrawable(R.mipmap.sp_dianzanhou_da,null)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return when (ev.action) {
            ACTION_DOWN -> {
                calculate(ev)
            }
            else -> super.dispatchTouchEvent(ev)
        }
    }

    private fun calculate(event: MotionEvent): Boolean {
        //获取点击时间
        val currTime = System.currentTimeMillis()
        //判断点击之间的时间差
        val interval = currTime - lastClickTime
        lastClickTime = currTime
        if (interval < INTERVAL) {//小于1秒，拦截事件，并做处理
            //绘制动画
            //获取当前的xy坐标值
            creatView(event.x, event.y)
            return true
        }
        return false
    }

    private fun creatView(x: Float, y: Float) {
        val imageView = ImageView(context)
        val lp = FrameLayout.LayoutParams(res!!.intrinsicWidth, res!!.intrinsicHeight)
        lp.leftMargin = (x - res!!.intrinsicWidth / 2).toInt()
        lp.topMargin = (y - res!!.intrinsicHeight / 2).toInt()
        imageView.scaleType = ImageView.ScaleType.MATRIX
        var newRes = res!!.mutate()
        DrawableCompat.setTint(newRes, randomColor())
        imageView.setImageDrawable(newRes)
        imageView.layoutParams = lp
        addView(imageView)
        addAnim(imageView)
    }

    private fun randomColor(): Int {
        val random = Random()
        val r = random.nextInt(255)
        val g = random.nextInt(255)
        val b = random.nextInt(255)
        return Color.rgb(r, g, b)
    }

    private fun addAnim(imageView: ImageView) {
        val mtrix = Matrix()

        mtrix.setScale(0.8f, 0.8f)
        imageView.imageMatrix = mtrix
        //缩放动画
        val scaleAnimation = ScaleAnimation(0.8f, 1.2f, 0.8f, 1.2f)
        val endDegress = 15 - Random().nextInt(30).toFloat()
        //旋转动画
        val rotateAnimation = RotateAnimation(0f, endDegress)
        //平移动画
        val translateAnimation = TranslateAnimation(0f, 0f, 0f, -150f)
        //渐变动画
        val alphaAnimation = AlphaAnimation(1.0f, 0f)

        val animationSet = AnimationSet(true)
        animationSet.addAnimation(scaleAnimation)
        animationSet.addAnimation(rotateAnimation)
        animationSet.addAnimation(translateAnimation)
        animationSet.addAnimation(alphaAnimation)
        imageView.animation = animationSet
        animationSet.duration = 2000
        animationSet.fillAfter = true
        animationSet.start()

        animationSet.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {

            }

            override fun onAnimationEnd(animation: Animation) {
                post { removeView(imageView) }

            }

            override fun onAnimationRepeat(animation: Animation) {

            }
        })

    }

}
